热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Python正则表达式学习记录及常用方法

本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。

在这里插入图片描述在这里插入图片描述学习记录,防止遗忘
在这里插入图片描述
正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配。

在这里插入图片描述
以上方法显的非常繁琐。。。

re 模块使 Python 语言拥有全部的正则表达式功能。

re.search 扫描整个字符串,匹配成功re.search方法返回一个匹配的对象,否则返回None。

re.search(pattern, string, flags=0)

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
The ’r‘ at the beginning of the pattern indicates that this is a rawstring. This means that Python interpreter shouldn’t try to interpret any special characters, and instead, should just pass the string to the function as is. In this example, there are no special characters. The rawstring and the normal string are exactly the same, but it’s a good idea to always use rawstrings for regular expressions in Python.

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
We can also pass additional options to the search function. For example, if we want our match to be case insensitive, we can do this by passing the re.IGNORECASE option.

We’ve seen by now how we can use a dot in our regular expressions as a special character that can match any character. In the regex world, this is known as a wildcard because it can match more than one character.
在这里插入图片描述
Sometimes we may want to match any characters that aren’t in a group. To do that, we use a circumflex inside the square brackets. For example, let’s create a search pattern that looks for any characters that’s not a letter.
在这里插入图片描述
If we want to match either one expression or another, we can use the pipe symbol to do that. This lets us list alternative options that can get matched. For example, we could have an expression that matches either the word cat or the word dog, like this.
在这里插入图片描述
RegEx concept, repeated matches. It’s quite common to see expressions that include a dot followed by a star. This means that it matches any character repeated as many times as possible including zero.
re* 匹配0个或多个的表达式
在这里插入图片描述
the Star takes as many characters as possible. In programming terms, we say that this behavior is greedy. It’s possible to modify the repetition qualifiers to make them less greedy. But we won’t get into that now.

Other implementations like the one used by Python or by the Egrep command include two additional repetition qualifiers plus and question mark, that can help us construct more complex expressions. Let’s check that out. The plus character matches one or more occurrences of the character that comes before it. So we had the pattern O plus L plus. Let’s check it against a few words.
In this case, there was one occurrence of each. In the match pattern shows us the shortest possible matching string.
re+ 匹配1个或多个的表达式
在这里插入图片描述
The question mark symbol is yet another multiplier. It means either zero or one occurrence of the character before it.
re? 匹配0个或1个由前面的正则表达式定义的片段,非贪婪方式
在这里插入图片描述
special characters:dot, star, plus, question mark, circumflex, dollar sign, and square brackets在这里插入图片描述
To match an actual dot, we need to use an Escape character, which in the case of regular expressions is a backslash character. So let’s add that to our pattern.在这里插入图片描述
It can get really confusing with backslashes since they’re also used to define some special string characters. We’ve called out, for example, that \n is a sequence using Python to indicate a new line, and \t does the same for tabs. When we see a pattern that includes a backslash, it could be escaping a special regex character or a special string character.
在这里插入图片描述
Using rawstrings, like we’ve been doing, helps avoid some of these possible confusion because the special characters won’t be interpreted when generating the string. They will only be interpreted when parsing the regular expression. On top of this, Python also uses the backslash for a few special sequences that we can use to represent predefined sets of characters. For example, \w matches any alphanumeric character including letters, numbers, and underscores. Let’s check out a couple of examples.
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
A great resource for testing out your regular expressions is a website called regex101.com. You can use this to try out your regexes, analyze each part of the expression, and figure out what’s up with them when they don’t work.
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
Python also offers numeric repetition qualifiers. These are written between curly brackets and can be one or two numbers specifying a range. For example, to match any string of exactly five letters, we can use an expression like this one:
在这里插入图片描述
Now we have an extra match for the word that’s actually longer. What if we wanted to match all the words that are exactly five letters long? We can do that using \b, which matches word limits at the beginning and end of the pattern, to indicate that we want full words, like this:
We can also have two numbers in the range. For example, if we wanted to match a range of five to ten letters or numbers, we could use an expression like this one:
在这里插入图片描述
在这里插入图片描述
Splitting and Replacing
在这里插入图片描述
Use a regular expression for searching in a plain string for replacing.
在这里插入图片描述
Use regular expressions for the replacing.
在这里插入图片描述
So once again we’d use parentheses to create capturing groups. In the first parameter, we’ve got an expression that contains the two groups that we want to match: one before the comma and one after the comma. We want to use a second parameter to replace the matching string. We use backslash two to indicate the second captured group followed by a space and backslash one to indicate the first captured group. When referring to captured groups, a backslash followed by a number indicates the corresponding captured group. This is a general notation for regular expressions, and it’s used by many tools that support regexes, not just Python.

Bonus:Regex Cross­word


推荐阅读
  • 本文讨论了clone的fork与pthread_create创建线程的不同之处。进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合。在调用系统调用fork创建一个进程时,子进程只是完全复制父进程的资源,这样得到的子进程独立于父进程,具有良好的并发性。但是二者之间的通讯需要通过专门的通讯机制,另外通过fork创建子进程系统开销很大。因此,在某些情况下,使用clone或pthread_create创建线程可能更加高效。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • 基于dlib的人脸68特征点提取(眨眼张嘴检测)python版本
    文章目录引言开发环境和库流程设计张嘴和闭眼的检测引言(1)利用Dlib官方训练好的模型“shape_predictor_68_face_landmarks.dat”进行68个点标定 ... [详细]
  • 本文由编程笔记#小编为大家整理,主要介绍了logistic回归(线性和非线性)相关的知识,包括线性logistic回归的代码和数据集的分布情况。希望对你有一定的参考价值。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • IB 物理真题解析:比潜热、理想气体的应用
    本文是对2017年IB物理试卷paper 2中一道涉及比潜热、理想气体和功率的大题进行解析。题目涉及液氧蒸发成氧气的过程,讲解了液氧和氧气分子的结构以及蒸发后分子之间的作用力变化。同时,文章也给出了解题技巧,建议根据得分点的数量来合理分配答题时间。最后,文章提供了答案解析,标注了每个得分点的位置。 ... [详细]
  • 本文介绍了解决二叉树层序创建问题的方法。通过使用队列结构体和二叉树结构体,实现了入队和出队操作,并提供了判断队列是否为空的函数。详细介绍了解决该问题的步骤和流程。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • 本文介绍了游标的使用方法,并以一个水果供应商数据库为例进行了说明。首先创建了一个名为fruits的表,包含了水果的id、供应商id、名称和价格等字段。然后使用游标查询了水果的名称和价格,并将结果输出。最后对游标进行了关闭操作。通过本文可以了解到游标在数据库操作中的应用。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 深度学习中的Vision Transformer (ViT)详解
    本文详细介绍了深度学习中的Vision Transformer (ViT)方法。首先介绍了相关工作和ViT的基本原理,包括图像块嵌入、可学习的嵌入、位置嵌入和Transformer编码器等。接着讨论了ViT的张量维度变化、归纳偏置与混合架构、微调及更高分辨率等方面。最后给出了实验结果和相关代码的链接。本文的研究表明,对于CV任务,直接应用纯Transformer架构于图像块序列是可行的,无需依赖于卷积网络。 ... [详细]
author-avatar
myq9395014
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有